﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TuanAnhPersonalWeb.Models;

namespace TuanAnhPersonalWeb.Classes
{
    public class PositionAccessConsolidator
    {
        TuanAnhEntities db = new TuanAnhEntities();
        public IList<PositionModel> getAllPosition()
        {
            IList<PositionModel> list = new List<PositionModel>();
            var data = db.TUANANH__POSITION.ToList();
            foreach (var item in data)
            {
                PositionModel model = new PositionModel();
                model.POS_ID = item.POS_ID;
                model.POS_DES = item.POS_DES;
                var soluong = db.TUANANH__EMPLOYEE.Where(e => e.POS_ID == item.POS_ID);
                model.Number = soluong.Count();
                if (item.LEADER_ID != null)
                {
                    model.LEADER_ID = item.LEADER_ID;
                    model.LEADER_NAME = db.TUANANH__EMPLOYEE.Where(e => e.EMP_ID == item.LEADER_ID).Select(e => e.EMP_NAME).Single();
                }
                else
                {
                    model.LEADER_NAME = "Chưa Có";
                }
                list.Add(model);
                
            }
            return list;
        }
        public PositionModel getPositionID(int id)
        {
            TUANANH__POSITION item = db.TUANANH__POSITION.Where(e => e.POS_ID == id).Single();
            PositionModel model = new PositionModel();
            model.POS_ID = item.POS_ID;
            model.POS_DES = item.POS_DES;
            if (item.LEADER_ID != null)
            {
                model.LEADER_ID = item.LEADER_ID;
            }
            else
            {
                model.LEADER_ID = null;
            }
            return model;


        }
        public IEnumerable<TUANANH__EMPLOYEE> getAllEmploy(int id)
        {
            var data = db.TUANANH__EMPLOYEE.Where(e=>e.POS_ID==id).Select(e => e).ToList();
            return data;
        }
        public void UpdatePosition(PositionModel item)
        {
            TUANANH__POSITION model = db.TUANANH__POSITION.Single(e => e.POS_ID == item.POS_ID);
            model.POS_DES = item.POS_DES;
            model.LEADER_ID = item.LEADER_ID;
            db.SaveChanges();
        }
    }
}